clear all
set more off
use "/Users/yasenov/Desktop/replication package/master_survey.dta"

************
************
************

gen male = (gender=="Male - Nam")

*Interpret in minutes
replace sleep_before = sleep_before*60

*Standard deviation of sleep and start time
by student_id: egen SDstarttime = sd(class_time)
by student_id: egen SDsleep = sd(sleep_before)

*We have information on days they didn't have class i.e. "regular sleep", need to think about how to use later
drop if class_time==0 | class_time==. | sleep_before==.

*Question 5 is academic satisfaction
gen satisfied = (q5=="Satisfied -  Hài lòng" | q5=="Very satisfied -  Rất hài lòng")
gen dissatisfied = (q5=="Unsatisfied - Không hài lòng" | q5=="Very unsatisfied - Rất không hài lòng")

*Generate outcome for missing class
gen missClass = (class_attendance!="Never")
* & class_attendance!="Sometimes"

sum sleep_before class_time

eststo clear
eststo: xi: reg sleep_before class_time, vce(cluster student_id)
estadd ysumm
eststo: xi: reg sleep_before class_time SDstarttime, vce(cluster student_id)
estadd ysumm
eststo: xi: reg SDsleep SDstarttime, vce(cluster student_id)
estadd ysumm
eststo: xi: reg dissatisfied SDstarttime class_time, vce(cluster student_id)
estadd ysumm
eststo: xi: reg missClass SDstarttime class_time, vce(cluster student_id)
estadd ysumm

la var class_time "Start Time"
la var SDstarttime "$ \sigma $(Start Time)"

quietly esttab using "SurveyResults.tex", ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	keep(class_time SDstarttime) nogaps ///
	stat(N ymean, labels("N (Student-day level)" "Mean of outcome") fmt(0 3 0)) ///
	mtitles("\begin{tabular}{@{}c@{}} Sleep duration: \\ Minutes of sleep \end{tabular}" "\begin{tabular}{@{}c@{}} Sleep duration: \\ Minutes of sleep \end{tabular}" "\begin{tabular}{@{}c@{}} Sleep irregularity: \\ $ \sigma $(Minutes of sleep) \end{tabular}" "\begin{tabular}{@{}c@{}} (Very) Dissatisfied with \\ academic performance? \end{tabular}" "\begin{tabular}{@{}c@{}} Always/often/sometimes \\ misses class? \end{tabular}") label    	
